<?php
	class utility{
		public static  function utf8convert($str) {

			if(!$str) return false;
		
			$utf8 = array(
		
					'a'=>'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ|Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
		
					'd'=>'đ|Đ',
		
					'e'=>'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ|É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
		
					'i'=>'í|ì|ỉ|ĩ|ị|Í|Ì|Ỉ|Ĩ|Ị',
		
					'o'=>'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ|Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
		
					'u'=>'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự|Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
		
					'y'=>'ý|ỳ|ỷ|ỹ|ỵ|Ý|Ỳ|Ỷ|Ỹ|Ỵ',
		
					);
		
			foreach($utf8 as $ascii=>$uni) $str = preg_replace("/($uni)/i",$ascii,$str);
		
			return $str;
		
		}
		
		public static  function utf8tourl($text){

			$text = strtolower(utility::utf8convert($text));
		
			$text = str_replace( "ß", "ss", $text);
		
			$text = str_replace( "%", "", $text);
		
			$text = preg_replace("/[^_a-zA-Z0-9 -]/", "",$text);
		
			$text = str_replace(array('%20', ' '), '-', $text);
		
			$text = str_replace("----","-",$text);
		
			$text = str_replace("---","-",$text);
		
			$text = str_replace("--","-",$text);
		
			return $text;
		
		}
		
		public static function getOrderStatus($status)
		{
			$temp = '';
			switch($status)
			{
				case '0' : $temp = 'Chưa xác nhận';
							break;
				case '1' : $temp = 'Đã xác nhận';
							break;
				case '2' : $temp = 'Đã thanh toán';
							break;
				case '3' : $temp = 'Hủy bởi khách hàng';
							break;
				case '4' : $temp = 'Hủy bởi Shoptivi';
							break;
			}
			
			return $temp;
		}
		
		
		
		//ham get link phan trang theo route (get route tren url)
		public static function currentLink()
		{
			
			$cur_url = $_SERVER['REQUEST_URI'];
			$link = $cur_url;
			if(isset($_REQUEST['page']))
			{
				$link = str_replace('&page='.$_REQUEST['page'],'',$link); 
				$link = str_replace('/page='.$_REQUEST['page'],'',$link); 
				//echo str_replace('&page=2','','/shoptivi/index.php?rt=admin_tivi/index&page=2');
			}
			
			
			$path = $_SERVER['PHP_SELF'];
			$path_parts = pathinfo($path);
			$link = str_replace($path_parts['dirname'].'/','',$link); 
			if($link == '') return __URL_BASE.'trang-chu.html';
			else
				return __URL_BASE.$link;
		}
		
		//ham hien thi phan trang theo page_size
		public static function pagination($page_size)
		{
			echo '<ul class="itemContainer ls">';
              
			//get route
			$route = (empty($_GET['rt'])) ? '' : $_GET['rt'];
			//get link theo route
			$link = utility::currentLink();
			//get current page
			$current_page = (isset($_REQUEST['page'])) ? $_REQUEST['page'] : 1; 
			  
			 if($current_page == '1')
			 {
             	echo '<li class="first"><span>← Trước</span></li>';
			 }
			 else
			 {
				echo '<li class="first"><a href="'.$link.'/page='.($current_page-1).'" class="jp-next jp-item" title=" - Trang 1">← Trước </a></li>';
			 }
             for($i=1;$i<=$page_size;$i++) : 
                 if($i == $current_page) :
              			echo '<li class="active"><span>'.$i.'</span></li>';
                 else : 
              			echo '<li><a href="'.$link.'/page='.$i.' " class="jp-undefined jp-item" title=" - Trang '.$i.'">'. $i .'</a></li>';
                 endif; 
             endfor; 
			 if($current_page == $page_size){
				  echo '<li class="first"><span>Tiếp theo →</span></li>';
			  }
			  else
			  {
				  echo '<li class="last"><a href="'.$link.'/page='.($current_page+1).'" class="jp-next jp-item" title=" - Trang 2">Tiếp theo →</a></li>';
			  }
              
            echo '</ul>';
		}
		
		public static function getPagination_admin($pages)
		{
			$link = utility::currentLink();
			echo '<div class="pagination">';	
			if($pages > 1){
				echo '<p>';
				$cur = (isset($_REQUEST['page'])) ? $_REQUEST['page'] : 1; 
				if($cur > 1){
				
					echo '<a href="'.$link.'&page='.($cur-1).'" ><< prev</a>';
			
				}
				else{
		
					echo '<span  class="disabled" ><< prev</span>';
				}
				for($i=1;$i<=$pages;$i++){
					if($cur == $i){
		
						echo '<span class="current" >'.$i.'</span>';
					
					}
					else{
		
						echo '<a href="'.$link.'&page='.$i.'">'.$i.'</a>';
					
					}
				}
				if($cur < $pages){
			
					echo '<a href="'.$link.'&page='.($cur+1).'" >next >></a>';
				
				}
				else{
			
					echo '<span  class="disabled" >next >></span>';
				
				}	
				echo '</p>';
			
			}
		
		
	 		echo '</div>';
      	}
		public static function setImageThumb($images,$images2,$images3,$images4,$images5){
			if($images != ''){
				return $images;	
			}
			else if($images2 != ''){
				return $images2;	
			}
			else if($images3 != ''){
				return $images3;	
			}
			else if($images4 != ''){
				return $images4;	
			}
			else if($images5 != ''){
				return $images5;	
			}
			else {
				return 'no_image.gif';	
			}
		}
	}
?>